import { BaseEntity } from '@libs/common/entities';
import { enumToObject } from '@libs/common/utils';
import { ApiProperty } from '@nestjs/swagger';
import { Column, Entity } from 'typeorm';

export enum StatusEnum {
  '下架' = 0,
  '上架' = 1,
}
const Status = [0, 1] as const;

const StatusComment =
  '商品状态：' +
  Object.keys(enumToObject(StatusEnum))
    .map((k) => `${k}: ${enumToObject(StatusEnum)[k]}`)
    .join('、');

@Entity('crm_node_market_product')
export class Product extends BaseEntity {
  @ApiProperty({ description: '商品名' })
  @Column({ comment: '商品名', unique: true })
  name: string;

  @ApiProperty({ description: '商品描述' })
  @Column({ comment: '商品描述' })
  description?: string;

  @ApiProperty({ description: '商品折扣价' })
  @Column({ type: 'int', comment: '商品折扣价', nullable: true })
  discountPrice?: number;

  @ApiProperty({ description: '商品原价' })
  @Column({ type: 'int', comment: '商品原价' })
  originalPrice: number;

  @ApiProperty({ description: '最低起购数量', default: 1 })
  @Column({ comment: '最低起购数量' })
  minBuy: number = 1;

  @ApiProperty({ description: '最大限购数量', default: 1 })
  @Column({ comment: '最大限购数量' })
  maxBuy?: number = 1;

  @ApiProperty({ description: '商品库存' })
  @Column({ comment: '商品库存', default: 0 })
  stock: number = 0;

  @ApiProperty({ description: '商品图片' })
  @Column({ comment: '商品图片' })
  image: string;

  @ApiProperty({ description: StatusComment, default: StatusEnum['上架'] })
  @Column('tinyint', {
    comment: StatusComment,
    default: StatusEnum['上架'],
  })
  status: (typeof Status)[number] = StatusEnum['上架'];
}
